﻿using ConsoleTest.Model;
using ConsoleTest.Tools;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleTest.Service
{
    public class Service
    {
        bool isReadFile = Convert.ToBoolean(ConfigurationManager.AppSettings["IsReadFile"]);
        GetHtml gh = new GetHtml();
        ExcelRed ed = new ExcelRed();

        StringBuilder sbTitle = new StringBuilder();
        public void Method360(List<KeyWord> list)
        {
            int count = 0;
            foreach (var keys in list)
            {
                //第一步通过关键词爬去360下拉推荐标题
                var url = string.Format("https://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word={0}&sid=894e63d393628b6742f1cbe8c52a467f&pq={1}", keys.Title, keys.Title);
                //关键词对应推荐的标题
                var html = gh.GetHtmlWithUtf(url, Encoding.UTF8);
                var roots = html.Substring(12, html.Length - 14).ToObject<Root360>();
                Console.WriteLine("关键词-----------'" + roots.query + "'---------\r\n");
                foreach (var item in roots.result)
                {
                    sbTitle.Append("insert into TitleWord(title,IsDropdown,Platform,parentid) values('" + item.word + "',1,'360'," + keys.Id + ");");
                }
                //第二部通过关键词爬去360底部相关搜索推荐标题

                var bottomUrl = string.Format("https://www.so.com/s?ie=utf-8&fr=none&src=360sou_newhome&q={0}", keys.Title);
                var bottomhtml = gh.GetHtmlWithUtf(bottomUrl, Encoding.UTF8);
                var hd = new HtmlDocument();
                hd.LoadHtml(bottomhtml);
                HtmlNode root = hd.DocumentNode;
                //获取 companyname
                HtmlNodeCollection nodeCompanyName = root.SelectNodes("//div[@id='rs']/table/tr/th");
                if (nodeCompanyName != null)
                {
                    foreach (var m in nodeCompanyName)
                    {
                        var aTag = m.InnerHtml;//a标签获取
                        Regex reg = new Regex(@"(?is)<a[^>]+?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");
                        MatchCollection mc = reg.Matches(aTag);
                        foreach (Match item in mc)
                        {
                            var title = item.Groups["text"].Value;//获取到title
                            foreach (var key in roots.result)
                            {
                                if (!key.word.Contains(title))
                                {
                                    sbTitle.Append("insert into TitleWord(title,IsDropdown,Platform,parentid) values('" + title + "',0,'360'," + keys.Id + ");");
                                }
                            }
                        }
                    }
                }
                Console.WriteLine("关键词采集完毕" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "第" + keys.Id + "条\r\n");
                count++;
                if (count % 100 == 0 || count == list.Count)
                {
                    string sqlTitle = sbTitle.ToString();
                    var countTitle = SqlHelper.ExecuteNonQuery(sqlTitle);
                    sqlTitle.Remove(0, sqlTitle.Length);
                }
            }
        }
    }
}
